//
/*   Helper code for Matrix * Vector operation program
*    Copyright (C) 2009 Goffredo Marocchi
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/



#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include <string.h>

#include <cutil_inline.h>
#include <cuda.h>
#include <cutil_math.h>
#include <vector_types.h>

#include "IGGS_CUDA_defines.h"

#include "MatTest_helper.h"

////////////////////////////////////////////////////
/////CUDA
////////////////////////////////////////////////////

/** \brief General data allocation function.
*
*	This function dynamically allocates a memory region of size data_size in device memory (GPU RAM)
*	using cudaMalloc().\n
*	This wrapper is useful to neatly check for errors.
*
*	\param d_ptr pointer to device memory, float*&.
*	\param data_size size of the memory region we want to allocate (in bytes), const int
*	\return void
*/
void Allocate_GPU_memory (float * &d_ptr, const int data_size) {

	cutilSafeCall( cudaMalloc((void **)&d_ptr, data_size)   );

	return;

}

/** \brief General data allocation function.
*
*	This function frees a dynamically allocated memory region in device memory (GPU RAM)
*	using cudaFree().\n
*	This wrapper is useful to neatly check for errors.
*
*	\param d_ptr pointer to device memory, float*.
*	\return void
*/
void Free_GPU_memory (float * d_ptr) {

	cutilSafeCall( cudaFree(d_ptr) );

	return;

}

/** \brief General data upload function.
*
*	This function uploads a block of host memory to device memory (main RAM --> GPU RAM).\n
*	Both memory regions must have been already allocated before this function is called.\n
*	This wrapper is useful to neatly check for errors.
*
*	\param d_ptr reference to the memory block in device memory, float*&.
*	\param h_ptr reference memory block in host memory, float*.
*	\param data_size size of the memory block we want to upload from host memory to device memory
*	\return void
*/
void Write_to_GPU (float * &d_ptr, float *h_ptr, const int data_size) {

	cutilSafeCall( cudaMemcpy(d_ptr, h_ptr, data_size, cudaMemcpyHostToDevice) );

	return;

}

/** \brief General data readback function.
*
*	This function transfers a block of device memory to host memory (GPU RAM --> main RAM).\n
*	Both memory regions must have been already allocated before this function is called.\n
*	This wrapper is useful to neatly check for errors.
*
*	\param d_ptr reference to the memory block in device memory, float*&.
*	\param h_ptr reference memory block in host memory, float*.
*	\param data_size size of the memory block we want to upload from host memory to device memory
*	\return void
*/
void Read_from_GPU (float *h_ptr,  float * &d_ptr, const int data_size) {

	cutilSafeCall( cudaMemcpy(h_ptr, d_ptr, data_size, cudaMemcpyDeviceToHost) );

	return;

}

/** \brief Texture object binding function.
*
* This function wrapper binds a device memory location to a  <Floating Point, 1D, cudaReadModeElementType>
* texture object.
* This wrapper is useful to neatly check for errors.
*
*	\param devPtr reference to the memory block in device memory, float*&.
*	\param texref reference to a texture object, texture <float, 1, cudaReadModeElementType> &
*	\param offset offset from the device memory base address, size_t*
*	\return void
*/
void BindTex1D_rmetFP (size_t *offset, texture <float, 1, cudaReadModeElementType> &texref, const void *devPtr) {

	cutilSafeCall( cudaBindTexture(offset, texref, devPtr));

	return;

}

/** \brief Texture object unbinding function.
*
* This function wrapper removes the binding between
* a device memory location and a  <Floating Point, 1D, cudaReadModeElementType>
* texture object.
* This wrapper is useful to neatly check for errors.
*
*	\param texref reference to a texture object, texture <float, 1, cudaReadModeElementType> &
*	\return void
*/
void UnbindTex1D_rmetFP (texture <float, 1, cudaReadModeElementType> &texref)  {

	  cutilSafeCall( cudaUnbindTexture(texref) );

	  return;

}


